package com.example.administrator.bighomework;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    ImageView vc_image;
    EditText etUsername,etPassword;
    Button btnLogin,btnRegist,vc_shuaixi, vc_ok;
    EditText vc_code;
    String getCode = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        vc_image = (ImageView) findViewById(R.id.vc_image);
        vc_image.setImageBitmap(Code.getInstance().getBitmap());
        vc_code = (EditText) findViewById(R.id.vc_code);
        getCode = Code.getInstance().getCode(); // 获取显示的验证码
        vc_shuaixi = (Button) findViewById(R.id.vc_shuaixi);


        // 获取界面中的相关View
        etUsername = (EditText) findViewById(R.id.etUsername);
        etPassword = (EditText) findViewById(R.id.etPassword);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnRegist =(Button) findViewById(R.id.btnRegist);
        // 设置登录按钮点击事件
        btnLogin.setOnClickListener(this);
        btnRegist.setOnClickListener(this);
        //给验证码刷新按钮注册监听器
        vc_shuaixi.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnLogin:
        // 获取用户输入的用户名和密码
        String username = etUsername.getText().toString();
        String password = etPassword.getText().toString();
        String v_code = vc_code.getText().toString().trim();
        // 非空验证
                if (v_code == null || v_code.equals("")) {
                    Toast.makeText(LoginActivity.this, "验证码为空", Toast.LENGTH_SHORT).show();
                } else if (!v_code.equals(getCode)) {
                    Toast.makeText(LoginActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(LoginActivity.this, "验证成功", Toast.LENGTH_SHORT).show();
                }
                if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();

                }


        // 使用BmobSDK提供的登录功能
        BmobUser user = new BmobUser();
        user.setUsername(username);
        user.setPassword(password);
        user.login(this, new SaveListener() {

            @Override
            public void onSuccess() {
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
            }
        });break;
            case R.id.btnRegist:
                Intent intent = new Intent(this,SignUpActivity.class);
                startActivity(intent);
                break;
            case R.id.vc_shuaixi:
                vc_image.setImageBitmap(Code.getInstance().getBitmap());
                getCode = Code.getInstance().getCode();
                break;
    }
}
}
//这是一段注释
//这是一段注释
//这是一段注释
//这是一段注释
